package com.mustang.core.client.type;

import com.google.gwt.core.client.JavaScriptObject;

/**
 * This is the simple extended JsArray for the Java Script Array instance in the
 * GWT world.
 * Supported value is as below: 
 * <@code>String.java</@code>
 * <@code>java.util.Date.java</@code>
 * <@code>Integer.java</@code>
 * <@code>Double.java</@code>
 * <@code>Boolean.java</@code>
 * <@code>JsObject.java</@code>
 *  
 * @author Jason.Cui
 */
//TODO:extend this class to support all the Function for Java Script Array ,such as push,reverse,sort,pop etcs.
public  final class JsArray<T> extends JavaScriptObject{
	protected JsArray()
	{
	}
	
	/**
	 * Return the empty JS Array <code>var a=[]</code> instance 
	 * 
	 * @param objs
	 * @return
	 */
	public static native final <T> JsArray<T> $A()/*-{
		return [];
	}-*/;
	
	
	/**
	 * Concat the target Array to the current Array It will create a new JsArray
	 * instance.
	 * 
	 * @param array
	 * @return a new Array instance
	 */
	public native JsArray<T> concat(JsArray<T> array)/*-{
		return this.concat(array);
	}-*/;
	
	/**
	 * Return the JS Array <code>var a=[]</code> instance by the given initial elements
	 * array.
	 * 
	 * @param objs
	 *          initial elements
	 * @return
	 */
	public static <T> JsArray<T> $A(T... elements){
		JsArray<T> a=$A();
		for(T t:elements){
			a.put(t);
		}
		return a;
	}
	
	public native JsArray<T> put(T s)/*-{
		this[this.length] =s;
		return this;
	}-*/;
	
	
	/**
	 * Convert each element of the array to a String and join them with a comma
	 * separator. The value returned from this method may vary between browsers
	 * based on how JavaScript values are converted into strings.
	 */
	public String join()
	{
		return join(",");
	}

	/**
	 * Convert each element of the array to a String and join them with a comma
	 * separator. The value returned from this method may vary between browsers
	 * based on how JavaScript values are converted into strings.
	 */
	public native String join(String separator) /*-{
		return this.join(separator);
	}-*/;

	public native int length() /*-{
		return this.length;
	}-*/;


	
	
	
}
